package com.hhl.redis.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hhl.redis.entity.Notice;
import com.hhl.redis.lock.LockRedis;
import com.hhl.redis.lock.RedisLock;
import com.hhl.redis.util.RedisUtil;

@RestController
@RequestMapping("/redis")
public class RedisTestController {
	@Autowired
	RedisUtil redisUtil;
	
	@Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
	
	//http://localhost:8080/redis/put?key=hhl&value=何海龙
	@ResponseBody
	@RequestMapping("/put")
	public Object put(@RequestParam("key")String key,@RequestParam("value")String value) {
		return redisUtil.set(key,value);
	}
	
	//http://localhost:8080/redis/get?key=hhl
	@ResponseBody
	@RequestMapping("/get")
	public Object get(@RequestParam("key")String  key) {
		return redisUtil.get(key);
	}
	
	//发布订阅测试  http://localhost:8080/redis/send
	//redis 127.0.0.1:6379>publish channel 'message'
	@ResponseBody
	@RequestMapping("/send")
	public void send() {
		// convertAndSend(String channel, Object message)
		// channel：主题名称，即是要发送的频道，message：消息体
		redisTemplate.convertAndSend("topicName", new Notice("DT", "hello world!"));
		System.out.println("发布者发送消息！");
	}
	
	//redis事务  http://localhost:8080/redis/transation?key=h&value=helloworld
	@ResponseBody
	@RequestMapping("/transation")
	public void transation(@RequestParam("key")String key,@RequestParam("value")String value) {
		// 开启事务权限
		redisTemplate.setEnableTransactionSupport(true);
		try {
			// 开启事务 begin
			redisTemplate.multi();
//			redisTemplate.opsForValue().set(key, value);
			redisUtil.set(key, value);
			System.out.println("存入完毕,马上开始提交redis事务");
//			int i = 1/0;
			// 提交事务
			List aa = redisTemplate.exec();
			System.out.println(aa.toString());
			System.out.println("提交事务成功");
		} catch (Exception e) {
			// 需要回滚事务
			System.out.println("准备回滚事务");
			redisTemplate.discard();
			System.out.println("回滚事务成功");
		}
	}
	
	@Autowired
	RedisLock redisLock;
	//分布式事务锁
	@ResponseBody
	@RequestMapping("/lock")
	public void lock() throws Exception {
//		if(redisLock==null) {
//			redisLock = new RedisLock();
//		}
//		String aaa = redisLock.lock("order", 10000);
//		System.out.println(aaa);
//		redisLock.release("order", aaa);
//		System.out.println(111);
		
		LockRedis lockRedis = new LockRedis();
		String identifier = lockRedis.lockWithTimeout("order", 5000l, 5000l);
		if (StringUtils.isEmpty(identifier)) {
			// 获取锁失败
			System.out.println(Thread.currentThread().getName() + ",获取锁失败，原因时间超时!!!");
			return;
		}
		System.out.println(Thread.currentThread().getName() + "获取锁成功,锁id identifier:" + identifier + "，执行业务逻辑");
		try {
			Thread.sleep(30);
		} catch (Exception e) {

		}
		// 释放锁
		boolean releaseLock = lockRedis.releaseLock("order", identifier);
		if (releaseLock) {
			System.out.println(Thread.currentThread().getName() + "释放锁成功,锁id identifier:" + identifier);
		}
	}

	public static void main(String[] args) {
		String a = "Aa";
		String b = "aA";
		System.out.println(a.hashCode()+","+b.hashCode());
		System.out.println(a.equals(b));
		
		Map<String,String> m = new HashMap<String,String>();
	}
}
